{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function, division, absolute_import\n",
    "from fontTools.ttLib import TTFont\n",
    "from fontTools.pens.basePen import BasePen\n",
    "from reportlab.graphics.shapes import Path\n",
    "from reportlab.lib import colors\n",
    "from reportlab.graphics import renderPM\n",
    "from reportlab.graphics.shapes import Group, Drawing, scale\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ReportLabPen(BasePen):\n",
    " \n",
    "    def __init__(self, glyphSet, path=None):\n",
    "        BasePen.__init__(self, glyphSet)\n",
    "        if path is None:\n",
    "            path = Path()\n",
    "        self.path = path\n",
    " \n",
    "    def _moveTo(self, p):\n",
    "        (x,y) = p\n",
    "        self.path.moveTo(x,y)\n",
    " \n",
    "    def _lineTo(self, p):\n",
    "        (x,y) = p\n",
    "        self.path.lineTo(x,y)\n",
    " \n",
    "    def _curveToOne(self, p1, p2, p3):\n",
    "        (x1,y1) = p1\n",
    "        (x2,y2) = p2\n",
    "        (x3,y3) = p3\n",
    "        self.path.curveTo(x1, y1, x2, y2, x3, y3)\n",
    " \n",
    "    def _closePath(self):\n",
    "        self.path.closePath()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def input_ttfToImage(fontName,imagePath,fmt=\"png\"):\n",
    "    font = TTFont(fontName)\n",
    "    gs = font.getGlyphSet()\n",
    "    glyphNames = font.getGlyphNames()\n",
    "    for i in glyphNames:\n",
    "        if i[0] == '.':#跳过'.notdef', '.null'\n",
    "            continue\n",
    "        \n",
    "        g = gs[i]\n",
    "        pen = ReportLabPen(gs, Path(fillColor=colors.black, strokeWidth=5))\n",
    "        g.draw(pen)\n",
    "        w=1024\n",
    "        h=1024\n",
    "        g = Group(pen.path)\n",
    "        g.translate(10,180)\n",
    "        \n",
    "        d = Drawing(w, h)\n",
    "        d.add(g)\n",
    "        imageFile = imagePath+\"/\"+i+\".png\"\n",
    "        renderPM.drawToFile(d, imageFile, fmt)\n",
    "        \n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Miniconda",
   "language": "python",
   "name": "tf1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
